﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using Zephyr.Core;
using Zephyr.Models;

namespace Zephyr.Areas.Mms.Controllers
{
    public class STOCKController : Controller
    {
        public ActionResult Index()
        {
            var code = new sys_codeService();
            List<object> depotl = new List<object>();
            depotl.Add(new {value="1064",text="成品库房"});
            depotl.Add(new {value="1062",text="原料库房"});
            var model = new
            {
                dataSource = new{
                    dsPricing = code.GetValueTextListByType("Pricing"),
                    depotList = depotl
                },
                urls = new{
                    query = "/api/Mms/STOCK",
                    newkey = "/api/Mms/STOCK/getnewkey",
                    edit = "/api/Mms/STOCK/edit" 
                },
                resx = new{
                    noneSelect = "请先选择一条数据！",
                    editSuccess = "保存成功！",
                    auditSuccess = "单据已审核！"
                },
                form = new{
                    BANTCHSN = "" ,
                    MATCODE = "" ,
                    SITE = "" ,
                    DEPOT = "" 
                },
                defaultRow = new {
                   
                },
                setting = new{
                    idField = "SMGUID",
                    postListFields = new string[] { "SMGUID","BANTCHSN" ,"MATCODE" ,"SITE" ,"DEPOT" ,"SQTY" ,"UQTY" ,"UNIT" ,"OWNER" ,"SGTAG" ,"USETAG" ,"PROVIDER" ,"INPRICE" ,"STOCKDATE" ,"UPDATETIME" ,"DESCRIPTION" ,"PACKSTYLE" ,"PASSQTY" ,"NOTPASSQTY" ,"FORCHECKQTY" ,"ALLOWQTY" ,"CLIENTTAG" ,"FORCLIENT" }
                }
            };

            return View(model);
        }
    }

    public class STOCKApiController : ApiController
    {
        public dynamic Get(RequestWrapper query)
        {
            query.LoadSettingXmlString(@"
<settings defaultOrderBy='SMGUID'>
    <select>*</select>
    <from>SM_STOCK</from>
    <where defaultForAll='true' defaultCp='equal' defaultIgnoreEmpty='true' >
        <field name='BANTCHSN'		cp='equal'></field>   
        <field name='MATCODE'		cp='equal'></field>   
        <field name='SITE'		cp='equal'></field>   
        <field name='DEPOT'		cp='equal'></field>   
    </where>
</settings>");
            var service = new SM_STOCKService();
            var pQuery = query.ToParamQuery();
            var result = service.GetDynamicListWithPaging(pQuery);
            return result;
        }

        public string GetNewKey()
        {
            return new SM_STOCKService().GetNewKey("SMGUID", "maxplus").PadLeft(6, '0'); ;
        }

        [System.Web.Http.HttpPost]
        public void Edit(dynamic data)
        {
            var listWrapper = RequestWrapper.Instance().LoadSettingXmlString(@"
<settings>
    <table>
        SM_STOCK
    </table>
    <where>
        <field name='SMGUID' cp='equal'></field>
    </where>
</settings>");
            var service = new SM_STOCKService();
            var result = service.Edit(null, listWrapper, data);
        }
    }
}
